package com.snowRemovingUser.util;

import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {
	/**
     * 格式化日期
     *
     * @param date
     * @return
     */
    public static String dateFormat(Date date, String format) {
    	if (date == null) {
    		return null;
    	}

        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(date);
    }

    /**
     * 字符串转日期
     *
     * @return
     */
    public static Date toDate(String dateStr, String format) {
    	if (!StringUtils.hasLength(dateStr)) {
    		return null;
    	}

    	Date date = null;
    	try {
    		SimpleDateFormat sdf = new SimpleDateFormat(format);
    		date = sdf.parse(dateStr);
    	} catch (Exception e) {

    	}

        return date;
    }

    /**
     * 系统时间取得
     *
     * @return
     */
    public static Date getSysDate() {
        Calendar calDefault = Calendar.getInstance();
        Date dateToday = calDefault.getTime();
        return dateToday;
    }

    /**
     * 指定格式的系统时间取得
     *
     * @param strDateFormat
     * @return
     */
    public static String getSysDate(String strDateFormat) {
        SimpleDateFormat sdf = null;
        String strNowDate = "";
        if (strDateFormat == null || strDateFormat.trim().length() == 0) {
            return strNowDate;
        }
        try {
            Date dateToday = getSysDate();
            sdf = new SimpleDateFormat(strDateFormat);
            strNowDate = sdf.format(dateToday);
        } catch (Exception e) {
            strNowDate = "";
            e.printStackTrace();
        } finally {
            if (sdf != null) {
                sdf = null;
            }
        }
        return strNowDate;
    }
}
